﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace WpfWordsLearner.Interop
{
	public class Global
	{
		internal const int BS_COMMANDLINK = 0x0000000E;
		private const uint BCM_SETNOTE = 0x00001609;
		private const uint BCM_SETSHIELD = 0x0000160C;

		[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern bool BringWindowToTop(IntPtr hwnd);

		[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern int SetActiveWindow(IntPtr hwnd);

		[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern int GetWindowThreadProcessId(IntPtr hwnd, IntPtr val);


		/// <summary>
		/// Override for SendMessage for setting the shield icon
		/// </summary>
		[DllImport("user32.dll", CharSet = CharSet.Unicode)]
		internal static extern IntPtr SendMessage(HandleRef hWnd, UInt32 Msg,
						IntPtr wParam, IntPtr lParam);

		/// <summary>
		/// Override for SendMessage for setting note text
		/// </summary>
		[DllImport("user32.dll", CharSet = CharSet.Unicode)]
		static extern IntPtr SendMessage(HandleRef hWnd, UInt32 Msg,
						IntPtr wParam, string lParam);

		/// <summary>
		/// Shows or hides the Vista shield icon on the specifed control
		/// </summary>
		/// The control on which to display the shield
		/// Indicates whether to show or hide the shield
		public static void SetShield(UIElement ctrl, bool showShield)
		{
			HwndSource source = (HwndSource)HwndSource.FromVisual(ctrl);
			IntPtr handle = source.Handle;

			if (handle != IntPtr.Zero)
				SendMessage(new HandleRef(ctrl, handle), BCM_SETSHIELD,
			IntPtr.Zero, new IntPtr(showShield ? 1 : 0));
		}

		/// <summary>
		/// Shows command link style note text on the specified control
		/// </summary>
		public static void SetNote(UIElement ctrl, string NoteText)
		{
			HwndSource source = (HwndSource)HwndSource.FromVisual(ctrl);
			IntPtr handle = source.Handle;

			if (handle != IntPtr.Zero)
				SendMessage(new HandleRef(ctrl, handle),
					BCM_SETNOTE, IntPtr.Zero, NoteText);
		}

		/// <summary>
		/// Returns true if the operating system is Vista or later
		/// </summary>
		/// <returns></returns>
		public static bool RunningVistaOrLater()
		{
			return System.Environment.OSVersion.Version.Major > 5;
		}
	}

	public class ResetManager
	{
		private const Int32 WM_QUERYENDSESSION = 0x0011;
		private const Int32 ENDSESSION_CLOSEAPP = 0x1;

		[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
		private static extern uint RegisterApplicationRestart(string pszCommandline, int dwFlags);

		///// <summary>
		///// Returns true if the specified windows message is a restart message
		///// </summary>
		///// The windows message to be checked
		///// <returns>True if it is a restart message</returns>
		//public static bool IsRestartMessage(System.Windows.Message msg)
		//{
		//    bool ret = false;
		//    if (msg.Msg == Global.WM_QUERYENDSESSION)
		//    {
		//        if ((msg.LParam.ToInt32() & Global.ENDSESSION_CLOSEAPP) ==
		//                Global.ENDSESSION_CLOSEAPP)
		//            ret = true;
		//    }
		//    return ret;
		//}

		/// <summary>
		/// Registers the currently running application for a restart message.
		/// </summary>
		/// <param name="restartCommandLine" />
		/// The application will be restarted with this command line string</param />
		public static void RegisterApplicationForRestart(string restartCommandLine)
		{
			//Can only do this in Vista
			if (Global.RunningVistaOrLater())
				RegisterApplicationRestart(restartCommandLine, 0);
		}
	}

}
